using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace DynamicDNS.Provider.Web
{
    /// <summary>
    /// Implementation for the webbased DynDNS Dynamic DNS Provider
    /// </summary>
    public class DynDNSProvider : DynamicDNS.Provider.BaseProvider.DynamicDNSWebBaseProvider, IDynamicDNSProvider
    {
        private const string URLTEMPLATE = "http://members.dyndns.org/nic/update?hostname={0}&myip={1}&wildcard=NOCHG";
		
            //"http://myip.us/cgi-bin/cgi_updateip?accountname=$account&password=$password&hostname=$subdomainname&domainname=$domainname&ipaddress=$ip";

        public DynDNSProvider()
        {
        }

        #region IDynamicDNSProvider Member

        public DynamicDNSUpdateResult UpdateIP(IPAddress ip, string yourhostname, string username, string password)
        {
            BuildRequestString(ip, yourhostname, username, password);
            return base.UpdateDNSEntry();
        }

        /// <summary>
        /// This is a helper method for replacing the template values against the correct values
        /// </summary>
        /// <param name="ip">Contains the IP Adress for which the Update is to processed</param>
        /// <param name="yourhostname">Contains the Hostname for which the Update is to processed. This is Provider specific.</param>
        /// <param name="username">Contains the required username for the update</param>
        /// <param name="password">Contains the required password for the update</param>
        private void BuildRequestString(IPAddress ip, string yourhostname, string username, string password)
        {
            Url = String.Format(URLTEMPLATE, yourhostname, ip.ToString());
            Headers.Add("Authorization", "Basic " + System.Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password)));
        }

        #endregion
    }
}
